﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using DBAccessLib;
using LineWatch.Common;
using System.Configuration;

namespace LineWatch_WinForm
{
    public class ConfigInfo
    {
        public static int CurrentLineCodeCount = 0;
        public static float ProductFontSizeTitle = 60;
        public static float ProductFontSizeCaption = 72;
        public static float ProductFontSizeValue = 85;
        public static float PositionFontSizeTitle = 60;
        public static float PositionFontSizeCaption = 72;
        public static float PositionFontSizeValue = 85;
        public static DataTable DtAllLines = null;
        public static DataTable DtAllPositions = null;
        public static DataTable DtLineWatchSys1 = null;
        public static DataTable DtLineWatchSys2 = null;
        public static DataTable DtLineWatchSys3 = null;

        public static string LineCode1 = string.Empty;
        public static string LineCode2 = string.Empty;
        public static string LineCode3 = string.Empty;

        public static int PositionNumInOneCommand = 20;
        
        /// <summary>
        /// Init Config Info
        /// </summary>
        public static void InitConfigInfo()
        {
            float tempSize = 0F;

            if (float.TryParse(ConfigurationManager.AppSettings["ProductFontSizeTitle"].ToString().Trim(), out tempSize))
            {
                ProductFontSizeTitle = tempSize;
            }

            if (float.TryParse(ConfigurationManager.AppSettings["ProductFontSizeCaption"].ToString().Trim(), out tempSize))
            {
                ProductFontSizeCaption = tempSize;
            }

            if (float.TryParse(ConfigurationManager.AppSettings["ProductFontSizeValue"].ToString().Trim(), out tempSize))
            {
                ProductFontSizeValue = tempSize;
            }


            if (float.TryParse(ConfigurationManager.AppSettings["PositionFontSizeTitle"].ToString().Trim(), out tempSize))
            {
                PositionFontSizeTitle = tempSize;
            }

            if (float.TryParse(ConfigurationManager.AppSettings["PositionFontSizeCaption"].ToString().Trim(), out tempSize))
            {
                PositionFontSizeCaption = tempSize;
            }

            if (float.TryParse(ConfigurationManager.AppSettings["PositionFontSizeValue"].ToString().Trim(), out tempSize))
            {
                PositionFontSizeValue = tempSize;
            }
        }

        /// <summary>
        /// Get Line Name By Code
        /// </summary>
        /// <param name="lineCode"></param>
        /// <returns></returns>
        public static string GetLineNameByCode(string lineCode)
        {
            foreach (DataRow row in ConfigInfo.DtAllLines.Rows)
            {
                if (string.Equals(lineCode.Trim(), row["Line_Code"].ToString().Trim()))
                {
                    return row["Line_Name"].ToString().Trim();
                }
            }

            return string.Empty;
        }

        /// <summary>
        /// Get Line Serial Port By Code
        /// </summary>
        /// <param name="lineCode"></param>
        /// <returns></returns>
        public static string GetLineSerialPortByCode(string lineCode)
        {
            foreach (DataRow row in ConfigInfo.DtAllLines.Rows)
            {
                if (string.Equals(lineCode.Trim(), row["Line_Code"].ToString().Trim()))
                {
                    return row["Serial_Port"].ToString().Trim();
                }
            }

            return string.Empty;
        }

        /// <summary>
        /// Get Shift Info
        /// </summary>
        /// <param name="lineWatchSys"></param>
        /// <returns></returns>
        public static string GetShiftInfo(DataTable lineWatchSys)
        {
            string shiftInfo = string.Empty;
            DateTime shiftAStartTime = ConfigInfo.GetShitAStartTime(lineWatchSys);
            DateTime shiftAEndTime = ConfigInfo.GetShitAEndTime(lineWatchSys);
            DateTime currentTime = ConfigInfo.GetCurrentTime();

            if ((currentTime >= shiftAStartTime)
                && (currentTime <= shiftAEndTime))
            {
                shiftInfo = "班次：1班";
            }
            else
            {
                shiftInfo = "班次：2班";
            }

            return shiftInfo;
        }

        /// <summary>
        /// Get Now Plan Info
        /// </summary>
        /// <param name="lineWatchSys"></param>
        /// <param name="planNum"></param>
        /// <returns></returns>
        public static string GetNowPlanInfo(DataTable lineWatchSys, long planNum)
        {
            long nowPlanNum;
            DateTime shiftAStartTime = ConfigInfo.GetShitAStartTime(lineWatchSys);
            DateTime shiftAEndTime = ConfigInfo.GetShitAEndTime(lineWatchSys);
            DateTime shiftBStartTime = ConfigInfo.GetShitBStartTime(lineWatchSys);
            DateTime shiftBEndTime = ConfigInfo.GetShitBEndTime(lineWatchSys);
            DateTime currentTime = DateTime.Now;

            if ((currentTime >= shiftAStartTime)
                && (currentTime <= shiftAEndTime))
            {
                // 班次：1班

                TimeSpan timeSpanTotal = shiftAEndTime - shiftAStartTime;
                TimeSpan timeSpanNow = currentTime - shiftAStartTime;

                nowPlanNum = (long)(timeSpanNow.TotalSeconds * planNum / timeSpanTotal.TotalSeconds);
            }
            else
            {
                // 班次：2班
                if (currentTime < shiftAStartTime)
                {
                    shiftBStartTime = shiftBStartTime.AddDays(-1);
                    shiftBEndTime = shiftBEndTime.AddDays(-1);
                }

                TimeSpan timeSpanTotal = shiftBEndTime - shiftBStartTime;
                TimeSpan timeSpanNow = currentTime - shiftBStartTime;

                nowPlanNum = (long)(timeSpanNow.TotalSeconds * planNum / timeSpanTotal.TotalSeconds);
            }

            return nowPlanNum.ToString();
        }

        /// <summary>
        /// Get ShitA Start Time
        /// </summary>
        /// <param name="lineWatchSys"></param>
        /// <returns></returns>
        public static DateTime GetShitAStartTime(DataTable lineWatchSys)
        {
            string strTime = string.Empty;

            foreach (DataRow row in lineWatchSys.Rows)
            {
                if (string.Equals("ShiftA", row["Function"].ToString().Trim()))
                {
                    strTime = row["Content1"].ToString().Trim();
                    break;
                }
            }

            string today = DateTime.Today.ToString("yyyy-MM-dd");
            string dayAndTime = today + " " + strTime;
            DateTime dateTime = DateTime.Parse(dayAndTime);

            return dateTime;
        }

        /// <summary>
        /// Get ShitA End Time
        /// </summary>
        /// <param name="lineWatchSys"></param>
        /// <returns></returns>
        public static DateTime GetShitAEndTime(DataTable lineWatchSys)
        {
            string strTime = string.Empty;

            foreach (DataRow row in lineWatchSys.Rows)
            {
                if (string.Equals("ShiftA", row["Function"].ToString().Trim()))
                {
                    strTime = row["Content2"].ToString().Trim();
                    break;
                }
            }

            string today = DateTime.Today.ToString("yyyy-MM-dd");
            string dayAndTime = today + " " + strTime;
            DateTime dateTime = DateTime.Parse(dayAndTime);

            return dateTime;
        }

        /// <summary>
        /// Get ShitB Start Time
        /// </summary>
        /// <param name="lineWatchSys"></param>
        /// <returns></returns>
        public static DateTime GetShitBStartTime(DataTable lineWatchSys)
        {
            string strTime = string.Empty;

            foreach (DataRow row in lineWatchSys.Rows)
            {
                if (string.Equals("ShiftB", row["Function"].ToString().Trim()))
                {
                    strTime = row["Content1"].ToString().Trim();
                    break;
                }
            }

            string today = DateTime.Today.ToString("yyyy-MM-dd");
            string dayAndTime = today + " " + strTime;
            DateTime dateTime = DateTime.Parse(dayAndTime);

            return dateTime;
        }

        /// <summary>
        /// Get ShitB End Time
        /// </summary>
        /// <param name="lineWatchSys"></param>
        /// <returns></returns>
        public static DateTime GetShitBEndTime(DataTable lineWatchSys)
        {
            string strTime = string.Empty;

            foreach (DataRow row in lineWatchSys.Rows)
            {
                if (string.Equals("ShiftB", row["Function"].ToString().Trim()))
                {
                    strTime = row["Content2"].ToString().Trim();
                    break;
                }
            }

            string today = DateTime.Today.AddDays(1).ToString("yyyy-MM-dd");
            string dayAndTime = today + " " + strTime;
            DateTime dateTime = DateTime.Parse(dayAndTime);

            return dateTime;
        }

        /// <summary>
        /// Get Current Time
        /// </summary>
        /// <returns></returns>
        public static DateTime GetCurrentTime()
        {
            string currentTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm");

            return DateTime.Parse(currentTime);
        }

        /// <summary>
        /// Init All Positions
        /// </summary>
        public static void InitAllPositions()
        {
            LinePositionModel linePositionModel = Position.GetAllLinePositions();
            DtAllPositions = linePositionModel.LinePositionTable;
        }

        /// <summary>
        /// Get Min Position
        /// </summary>
        /// <param name="lineCode"></param>
        /// <returns></returns>
        public static int GetMinPosition(string lineCode)
        {
            int minDMPosition = 99999999;

            if (ConfigInfo.DtAllPositions != null)
            {
                foreach (DataRow row in ConfigInfo.DtAllPositions.Rows)
                {
                    int dmPosition = 0;

                    if (string.Equals(lineCode.Trim(), row["Line_Code"].ToString().Trim()))
                    {
                        if (int.TryParse(row["Content1"].ToString().Trim(), out dmPosition))
                        {
                            if (minDMPosition > dmPosition)
                            {
                                minDMPosition = dmPosition;
                            }
                        }
                    }
                }
            }

            return minDMPosition;
        }

        /// <summary>
        /// Get Max Position
        /// </summary>
        /// <param name="lineCode"></param>
        /// <returns></returns>
        public static int GetMaxPosition(string lineCode)
        {
            int maxDMPosition = 0;

            if (ConfigInfo.DtAllPositions != null)
            {
                foreach (DataRow row in ConfigInfo.DtAllPositions.Rows)
                {
                    int dmPosition = 0;

                    if (string.Equals(lineCode.Trim(), row["Line_Code"].ToString().Trim()))
                    {
                        if (int.TryParse(row["Content1"].ToString().Trim(), out dmPosition))
                        {
                            if (maxDMPosition < dmPosition)
                            {
                                maxDMPosition = dmPosition;
                            }
                        }
                    }
                }
            }

            return maxDMPosition;
        }

        /// <summary>
        /// Get Alarm Music Name
        /// </summary>
        /// <param name="lineWatchSys"></param>
        /// <returns></returns>
        public static string GetAlarmMusicName(DataTable lineWatchSys)
        {
            string alarmMusicName = string.Empty;

            foreach (DataRow row in lineWatchSys.Rows)
            {
                if (string.Equals("MusicA", row["Function"].ToString().Trim()))
                {
                    alarmMusicName = row["Content1"].ToString().Trim();
                    break;
                }
            }

            return alarmMusicName;
        }

        /// <summary>
        /// Get Help Music Name
        /// </summary>
        /// <param name="lineWatchSys"></param>
        /// <returns></returns>
        public static string GetHelpMusicName(DataTable lineWatchSys)
        {
            string helpMusicName = string.Empty;

            foreach (DataRow row in lineWatchSys.Rows)
            {
                if (string.Equals("MusicH", row["Function"].ToString().Trim()))
                {
                    helpMusicName = row["Content1"].ToString().Trim();
                    break;
                }
            }

            return helpMusicName;
        }

        /// <summary>
        /// Get Stop Music Name
        /// </summary>
        /// <param name="lineWatchSys"></param>
        /// <returns></returns>
        public static string GetStopMusicName(DataTable lineWatchSys)
        {
            string stopMusicName = string.Empty;

            foreach (DataRow row in lineWatchSys.Rows)
            {
                if (string.Equals("MusicS", row["Function"].ToString().Trim()))
                {
                    stopMusicName = row["Content1"].ToString().Trim();
                    break;
                }
            }

            return stopMusicName;
        }

        /// <summary>
        /// Get Screen Time Span 1
        /// </summary>
        /// <param name="lineWatchSys"></param>
        /// <returns></returns>
        public static int GetScreenTimeSpan1(DataTable lineWatchSys)
        {
            string screenTimeSpan = string.Empty;

            foreach (DataRow row in lineWatchSys.Rows)
            {
                if (string.Equals("ScreenT", row["Function"].ToString().Trim()))
                {
                    screenTimeSpan = row["Content1"].ToString().Trim();
                    break;
                }
            }

            int temp = 0;
            if (int.TryParse(screenTimeSpan, out temp))
            {
                return int.Parse(screenTimeSpan);
            }

            return 0;
        }

        /// <summary>
        /// Get Screen Time Span 2
        /// </summary>
        /// <param name="lineWatchSys"></param>
        /// <returns></returns>
        public static int GetScreenTimeSpan2(DataTable lineWatchSys)
        {
            string screenTimeSpan = string.Empty;

            foreach (DataRow row in lineWatchSys.Rows)
            {
                if (string.Equals("ScreenT", row["Function"].ToString().Trim()))
                {
                    screenTimeSpan = row["Content2"].ToString().Trim();
                    break;
                }
            }

            int temp = 0;
            if (int.TryParse(screenTimeSpan, out temp))
            {
                return int.Parse(screenTimeSpan);
            }

            return 0;
        }

        /// <summary>
        /// Get Screen Picture Name 1
        /// </summary>
        /// <param name="lineWatchSys"></param>
        /// <returns></returns>
        public static string GetScreenPictureName1(DataTable lineWatchSys)
        {
            string screenPictureName = string.Empty;

            foreach (DataRow row in lineWatchSys.Rows)
            {
                if (string.Equals("ScreenP", row["Function"].ToString().Trim()))
                {
                    screenPictureName = row["Content1"].ToString().Trim();
                    break;
                }
            }

            return screenPictureName;
        }

        /// <summary>
        /// Get Screen Picture Name 2
        /// </summary>
        /// <param name="lineWatchSys"></param>
        /// <returns></returns>
        public static string GetScreenPictureName2(DataTable lineWatchSys)
        {
            string screenPictureName = string.Empty;

            foreach (DataRow row in lineWatchSys.Rows)
            {
                if (string.Equals("ScreenP", row["Function"].ToString().Trim()))
                {
                    screenPictureName = row["Content2"].ToString().Trim();
                    break;
                }
            }

            return screenPictureName;
        }

        /// <summary>
        /// Get Screen Picture Name 3
        /// </summary>
        /// <param name="lineWatchSys"></param>
        /// <returns></returns>
        public static string GetScreenPictureName3(DataTable lineWatchSys)
        {
            string screenPictureName = string.Empty;

            foreach (DataRow row in lineWatchSys.Rows)
            {
                if (string.Equals("ScreenP", row["Function"].ToString().Trim()))
                {
                    screenPictureName = row["Content3"].ToString().Trim();
                    break;
                }
            }

            return screenPictureName;
        }

        /// <summary>
        /// Get Screen Picture Name 4
        /// </summary>
        /// <param name="lineWatchSys"></param>
        /// <returns></returns>
        public static string GetScreenPictureName4(DataTable lineWatchSys)
        {
            string screenPictureName = string.Empty;

            foreach (DataRow row in lineWatchSys.Rows)
            {
                if (string.Equals("ScreenP", row["Function"].ToString().Trim()))
                {
                    screenPictureName = row["Content4"].ToString().Trim();
                    break;
                }
            }

            return screenPictureName;
        }
    }
}